#include "sk_game_item_property.h"
#include "sk_game_item.h"
#include <QLineEdit>
#include <QComboBox>
#include <QDoubleValidator>
#include <QPainter>
#include <climits>
#include <QColor>
#include <QPalette>
#include <QColorDialog>
#include "biteditor.h"

#include <QMessageBox>

namespace sk
{

GameItemProperty::GameItemProperty(GameItem * i, QString n)
{
    item = i;
    name = n;
}

QString GameItemProperty::valueToString()
{
    return QString("Convertion error");
}

QWidget * GameItemProperty::createEditor(QWidget * parent)
{
    return 0;
}

void GameItemProperty::setEditorData(QWidget *)
{
}

void GameItemProperty::setPropertyData(QWidget *)
{
}

void GameItemProperty::paint(QPainter * painter, const QStyleOptionViewItem &option)
{
    painter->save();

    QString strColor = valueToString();
    if ('#' == strColor[0])
    {
        painter->setBrush(QBrush(QColor(valueToString())));
        painter->drawRect(option.rect);
    }

    painter->drawText(option.rect, valueToString(), QTextOption(Qt::AlignLeft | Qt::AlignVCenter));
    painter->restore();
}

GameItemFloatProperty::GameItemFloatProperty(GameItem * i, QString n, float * d)
    :
GameItemProperty(i, n),
data(d)
{
}

QString GameItemFloatProperty::valueToString()
{
    return QString::number(*data);
}

QWidget * GameItemFloatProperty::createEditor(QWidget * parent)
{
    QLineEdit * res = new QLineEdit(parent);
    QDoubleValidator * v = new QDoubleValidator(res);
    res->setValidator(v);
    return res;
}

void GameItemFloatProperty::setEditorData(QWidget * editor)
{
    QLineEdit * e = static_cast<QLineEdit*>(editor);
    e->setText(valueToString());
}

void GameItemFloatProperty::setPropertyData(QWidget * editor)
{
    QLineEdit * e = static_cast<QLineEdit*>(editor);
    *data = e->text().toFloat();
    item->onPropertyChange(this);
}

GameItemIntProperty::GameItemIntProperty(GameItem * i, QString n, int * d)
    :
GameItemProperty(i, n),
data(d),
bitEditor(false)
{

}

QString GameItemIntProperty::valueToString()
{
    return QString::number(*data);
}

QWidget * GameItemIntProperty::createEditor(QWidget * parent)
{
    if (!bitEditor)
    {
        QLineEdit * res = new QLineEdit(parent);
        QIntValidator * v = new QIntValidator(res);
        res->setValidator(v);

        return res;
    }
    else
    {
        BitEditor * be = new BitEditor(numBits, parent);
        be->setBitsValue(*data);
        return be;
    }
}

void GameItemIntProperty::setEditorData(QWidget * editor)
{
    if (!bitEditor)
    {
        QLineEdit * e = static_cast<QLineEdit*>(editor);
        e->setText(valueToString());
    }
    else
    {

    }
}

void GameItemIntProperty::setPropertyData(QWidget * editor)
{
    if (!bitEditor)
    {
        QLineEdit * e = static_cast<QLineEdit*>(editor);
        *data = e->text().toInt();
        item->onPropertyChange(this);
    }
    else
    {

        BitEditor * be = static_cast<BitEditor*>(editor);
        *data = be->getBitsValue().toInt();
        item->onPropertyChange(this);
    }

}

GameItemShortProperty::GameItemShortProperty(GameItem * i, QString n, short * d)
    :
GameItemProperty(i, n),
data(d)
{
}

QString GameItemShortProperty::valueToString()
{
    return QString::number(*data);
}

QWidget * GameItemShortProperty::createEditor(QWidget * parent)
{
    QLineEdit * res = new QLineEdit(parent);
    QIntValidator * v = new QIntValidator(0, SHRT_MAX, res);
    res->setValidator(v);
    return res;
}

void GameItemShortProperty::setEditorData(QWidget * editor)
{
    QLineEdit * e = static_cast<QLineEdit*>(editor);
    e->setText(valueToString());
}

void GameItemShortProperty::setPropertyData(QWidget * editor)
{
    QLineEdit * e = static_cast<QLineEdit*>(editor);
    *data = e->text().toShort();
    item->onPropertyChange(this);
}

GameItemEnumProperty::GameItemEnumProperty(GameItem * i, QString n, int * d, QStringList& ns, QVector<int>& es)
    :
GameItemProperty(i, n),
data(d),
names(ns),
enums(es)
{
}

QString GameItemEnumProperty::valueToString()
{
    for (int i = 0; i < enums.size(); ++i)
    {
        if (enums[i] == *data)
        {
            return names[i];
        }
    }

    Q_ASSERT(false);
    return "Error!";
}

QWidget * GameItemEnumProperty::createEditor(QWidget * parent)
{
    QComboBox * res = new QComboBox(parent);
    for (int i = 0; i < names.size(); ++i)
    {
        res->addItem(names[i]);
    }
    return res;
}

void GameItemEnumProperty::setEditorData(QWidget * editor)
{
    QComboBox * e = static_cast<QComboBox*>(editor);
    for (int i = 0; i < enums.size(); ++i)
    {
        if (enums[i] == *data)
        {
            e->setCurrentIndex(i);
            return;
        }
    }
}

void GameItemEnumProperty::setPropertyData(QWidget * editor)
{
    QComboBox * e = static_cast<QComboBox*>(editor);
    *data = enums[e->currentIndex()];
    item->onPropertyChange(this);
}


GameItemBoolProperty::GameItemBoolProperty(GameItem * i, QString n, bool * d)
    :
GameItemProperty(i, n),
data(d)
{
}

QString GameItemBoolProperty::valueToString()
{
    return *data ? "True" : "False";
}

QWidget * GameItemBoolProperty::createEditor(QWidget * parent)
{
    QComboBox * res = new QComboBox(parent);
    res->addItem("False");
    res->addItem("True");
    return res;
}

void GameItemBoolProperty::setEditorData(QWidget * editor)
{
    QComboBox * e = static_cast<QComboBox*>(editor);
    e->setCurrentIndex(*data ? 1 : 0);
}

void GameItemBoolProperty::setPropertyData(QWidget * editor)
{
    QComboBox * e = static_cast<QComboBox*>(editor);
    *data = e->currentIndex() == 1 ? true : false;
    item->onPropertyChange(this);
}

GameItemColorProperty::GameItemColorProperty(GameItem * i, QString n, QColor * d)
    :
    GameItemProperty(i, n),
    data(d)
{

}

QString GameItemColorProperty::valueToString()
{
    return data->name();
}


QWidget * GameItemColorProperty::createEditor(QWidget * parent)
{
    QLabel * res = new QLabel(parent);
    res->setFrameStyle(Qt::NoTextInteraction);
    QColor color = QColorDialog::getColor(Qt::yellow);
    if (color.isValid())  res->setText(color.name());
    else res->setText(data->name());
    return res;
}

void GameItemColorProperty::setEditorData(QWidget * editor)
{
    QLabel * e = static_cast<QLabel*>(editor);
    QPalette  pal;
    pal.setColor(QPalette::Base, QColor(e->text()));
    e->setPalette(pal);
    *data = QColor(e->text());
    item->onPropertyChange(this);

}

void GameItemColorProperty::setPropertyData(QWidget * editor)
{
    QLabel * e = static_cast<QLabel*>(editor);
    *data = QColor(e->text());
    item->onPropertyChange(this);
}


} // namespace sk
